NONE_IMAGE=`/usr/bin/docker images | grep 'none' | awk -F ' ' '{print $3}'`
if [ -n "${NONE_IMAGE}" ]; then
        # check docker container
        for dc in `/usr/bin/docker ps -a | grep ${NONE_IMAGE} | awk -F " " '{print $1}'`
        do
                echo "Stop container: ${dc}"
                /usr/bin/docker stop ${dc}
                # delete while docker container was exists
                echo "##Delete exists Container_Id: "${dc}
                /usr/bin/docker rm ${dc}
        done

        # delete while docker image was exists
        echo "##Delete exists Image: "${NONE_IMAGE}
        /usr/bin/docker rmi ${NONE_IMAGE}
fi

DOCKER_NAME=houxin
DOCKER_IMAGE=`/usr/bin/docker images | grep ${DOCKER_NAME} | awk -F ' ' '{print $3}'`
if [ -n "${DOCKER_IMAGE}" ]; then
        # check docker container
        for dc in `/usr/bin/docker ps -a | grep ${DOCKER_NAME} | awk -F " " '{print $1}'`
        do
                echo "Stop container: ${dc}"
                /usr/bin/docker stop ${dc}
                # delete while docker container was exists
                echo "##Delete exists Container_Id: "${dc}
                /usr/bin/docker rm ${dc}
        done

        # delete while docker image was exists
        echo "##Delete exists Image: "${DOCKER_IMAGE}
        /usr/bin/docker rmi ${DOCKER_IMAGE}
fi
echo ""
if [ ! $GIT_PREVIOUS_SUCCESSFUL_COMMIT ];then
    echo "GIT_PREVIOUS_SUCCESSFUL_COMMIT is not exists."
    exit 0
else
    echo "GIT_COMMIT=[$GIT_COMMIT],GIT_PREVIOUS_SUCCESSFUL_COMMIT=[$GIT_PREVIOUS_SUCCESSFUL_COMMIT]"
    if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ];then
        echo "GIT_COMMIT is equals to GIT_PREVIOUS_SUCCESSFUL_COMMIT,skip build."

    else
        echo "GIT_COMMIT is not equals to GIT_PREVIOUS_SUCCESSFUL_COMMIT"
         for dn in houxin-register houxin-gateway houxin-auth houxin-admin
           do
               echo ====================  $dn  ====================
               echo ====================  $PWD  ====================
               echo ====================   building  houxin:5000/${dn}:v${BUILD_ID}  ====================
               docker build -f ${dn}/Dockerfile -t houxin:5000/${dn}:v${BUILD_ID} .
               echo ' ====================build success! ===================='
               docker push houxin:5000/${dn}:v${BUILD_ID}
         done
        exit 0
    fi
fi


